// src/api/BusApi.ts
import { BaseApiClient } from '../BaseApi.ts';
import type { BusData, ApiResponse } from '../../types/DataTypes.ts';

// 巴士相关API
export class BusApi extends BaseApiClient {
    constructor() {
        super();
    }

    // 获取所有巴士
    async getAllBuses(): Promise<ApiResponse<BusData[]>> {
        return this.request<BusData[]>({
            method: 'GET',
            url: '/buses',
        });
    }

    // 搜索巴士
    async searchBuses(location: string): Promise<ApiResponse<BusData[]>> {
        return this.request<BusData[]>({
            method: 'GET',
            url: '/buses/search',
            params: { location },
        });
    }

    // 添加新巴士
    async addBus(bus: Omit<BusData, 'availBus'> & { availBus?: number }): Promise<ApiResponse<BusData>> {
        return this.request<BusData>({
            method: 'POST',
            url: '/buses',
            data: {
                ...bus,
                availBus: bus.availBus ?? bus.totalBus, // 默认可用数量等于总数
            },
        });
    }

    // 更新巴士信息
    async updateBus(busData: BusData): Promise<ApiResponse> {
        return this.request({
            method: 'PUT',
            url: '/buses/update',
            data: busData,
        });
    }

    // 删除巴士
    async deleteBus(location: string): Promise<ApiResponse> {
        return this.request({
            method: 'DELETE',
            url: `/buses/${encodeURIComponent(location)}`,
        });
    }

}

// 创建单例实例
export const busApi = new BusApi();

// 默认导出
export default busApi;